/*
 * Copyright © 2011 SenseForce.com.
 * The Software shall be used for Good, not Evil.
 */

package com.senseforce.classes;

import javax.microedition.lcdui.*;
import java.util.Vector;
import com.senseforce.midlets.PhoneBook;

/**
 *
 * @author jcccn
 * @site http://www.senseforce.com/
 */
public class PhoneBookList extends List implements CommandListener {
	
	private PhoneBook midlet = null;
	private Command backCommand = new Command("Back", Command.BACK, 0);
	private Command addCommand = new Command("Add", Command.SCREEN, 0);
	private Command delCommand = new Command("Del", Command.SCREEN, 1);
	
	public PhoneBookList(PhoneBook midlet) {
		super("Contacts", List.IMPLICIT);
		this.midlet = midlet;
		setCommandListener(this);
		addCommand(backCommand);
		addCommand(addCommand);
		addCommand(delCommand);
	}
	
	/*刷新载入所有电话*/
	public void refresh(){
		this.deleteAll();		
		Vector v = PhoneBookRMSManager.getAllContacts();
		for(int i=0;i<v.size();i++){
			PhoneBookContact contact = (PhoneBookContact)v.elementAt(i);	
			String content = contact.getCname() + ":" + contact.getPhone();
			this.append(content, null);
		}
		if(v.size() == 0) {
			removeCommand(delCommand);
		}
		else {
			addCommand(delCommand);
		}
	}
	
	public void commandAction(Command c, Displayable d) {
		if (c == backCommand) {
			midlet.changeForm(PhoneBookConfig.FORMTYPE_WELCOME);
		}
		else if (c == addCommand) {
			midlet.changeForm(PhoneBookConfig.FORMTYPE_PHONEADD);
		}
		else if (c == delCommand) {
			PhoneBookRMSManager.delContact(getString(getSelectedIndex()));
			midlet.changeForm(PhoneBookConfig.FORMTYPE_PHONELIST);
		}
	}
}
